home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2003 August / MW 8 2003 CD1.iso / Inside Macworld / Product News / gimp-1.2.4.sit / gimp-1.2.4 / plug-ins / imagemap / imap_selection.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-11-05  |  12.8 KB  |  433 lines

  1. /*
  2.  * This is a plug-in for the GIMP.
  3.  *
  4.  * Generates clickable image maps.
  5.  *
  6.  * Copyright (C) 1998-1999 Maurits Rijk  lpeek.mrijk@consunet.nl
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21.  *
  22.  */
  23.  
  24. #include "config.h"
  25.  
  26. #include <stdio.h>
  27.  
  28. #include "imap_cmd_edit_object.h"
  29. #include "imap_cmd_select.h"
  30. #include "imap_cmd_unselect.h"
  31. #include "imap_cmd_unselect_all.h"
  32. #include "imap_edit_area_info.h"
  33. #include "imap_main.h"
  34. #include "imap_misc.h"
  35. #include "imap_selection.h"
  36.  
  37. #include "libgimp/stdplugins-intl.h"
  38.  
  39. #include "arrow_up.xpm"
  40. #include "arrow_down.xpm"
  41. #include "delete.xpm"
  42. #include "edit.xpm"
  43.  
  44. static void
  45. set_buttons(Selection_t *data)
  46. {
  47.    if (data->selected_child) {
  48.       gtk_widget_set_sensitive(data->arrow_up, 
  49.                    (data->selected_row) ? TRUE : FALSE);
  50.       if (data->selected_row < GTK_CLIST(data->list)->rows - 1) 
  51.      gtk_widget_set_sensitive(data->arrow_down, TRUE);
  52.       else
  53.      gtk_widget_set_sensitive(data->arrow_down, FALSE);
  54.       gtk_widget_set_sensitive(data->remove, TRUE);
  55.       gtk_widget_set_sensitive(data->edit, TRUE);
  56.    } else {
  57.       gtk_widget_set_sensitive(data->arrow_up, FALSE);
  58.       gtk_widget_set_sensitive(data->arrow_down, FALSE);
  59.       gtk_widget_set_sensitive(data->remove, FALSE);
  60.       gtk_widget_set_sensitive(data->edit, FALSE);
  61.    }
  62. }
  63.  
  64. static void
  65. select_row_cb(GtkWidget *widget, gint row, gint column, GdkEventButton *event,
  66.           Selection_t *data)
  67. {
  68.    data->selected_child = widget;
  69.    data->selected_row = row;
  70.  
  71.    set_buttons(data);
  72.    if (data->select_lock) {
  73.       data->select_lock = FALSE;
  74.    } else {
  75.       Object_t *obj = gtk_clist_get_row_data(GTK_CLIST(data->list), row);
  76.       Command_t *command;
  77.       /* Note: event can be NULL if select_row_cb is called as a result of
  78.      a key press! */
  79.       if (event && event->state & GDK_SHIFT_MASK) {
  80.      command = select_command_new(obj);
  81.       } else {
  82.      Command_t *sub_command;
  83.      
  84.      command = subcommand_start(NULL);
  85.      sub_command = unselect_all_command_new(data->object_list, NULL);
  86.      command_add_subcommand(command, sub_command);
  87.      sub_command = select_command_new(obj);
  88.      command_add_subcommand(command, sub_command);
  89.      command_set_name(command, sub_command->name);
  90.      subcommand_end();
  91.       }
  92.       command_execute(command);
  93.       redraw_preview();        /* Fix me! */
  94.    }
  95. }
  96.  
  97. static void
  98. unselect_row_cb(GtkWidget *widget, gint row, gint column, 
  99.         GdkEventButton *event, Selection_t *data)
  100. {
  101.    if (data->unselect_lock) {
  102.       data->unselect_lock = FALSE;
  103.    } else {
  104.       Object_t *obj = gtk_clist_get_row_data(GTK_CLIST(data->list), row);
  105.       Command_t *command;
  106.       command = unselect_command_new(obj);
  107.       command_execute(command);
  108.       redraw_preview();        /* Fix me! */
  109.    }
  110.    data->selected_child = NULL;
  111.    set_buttons(data);
  112. }
  113.  
  114. static void
  115. row_move_cb(GtkWidget *widget, gint src, gint des)
  116. {
  117.    printf("move: %d %d\n", src, des);
  118. }
  119.  
  120. static gboolean doubleclick;
  121.  
  122. static void
  123. button_press_cb(GtkWidget *widget, GdkEventButton *event, Selection_t *data)
  124. {
  125.    if (event->button == 1) {
  126.       if (doubleclick) {
  127.      gint row, column;
  128.      doubleclick = FALSE;
  129.      if (gtk_clist_get_selection_info(GTK_CLIST(widget), (gint) event->x, 
  130.                       (gint) event->y, &row, &column)) {
  131.         Object_t *obj = gtk_clist_get_row_data(GTK_CLIST(data->list), row);
  132.         object_edit(obj, TRUE);
  133.      }
  134.       } else {
  135.      doubleclick = TRUE;
  136.       }
  137.    }
  138. }
  139.  
  140. static void
  141. button_release_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
  142. {
  143.    if (event->button == 1)
  144.       doubleclick = FALSE;
  145. }
  146.  
  147. static void
  148. selection_command(GtkWidget *widget, gpointer data)
  149. {
  150.    CommandFactory_t *factory = (CommandFactory_t*) data;
  151.    Command_t *command = (*factory)();
  152.    command_execute(command);
  153. }
  154.  
  155. static GtkWidget*
  156. make_selection_toolbar(Selection_t *data, GtkWidget *window)
  157. {
  158.    GtkWidget *toolbar;
  159.  
  160.    toolbar = gtk_toolbar_new(GTK_ORIENTATION_VERTICAL, GTK_TOOLBAR_ICONS);
  161.    gtk_container_set_border_width(GTK_CONTAINER(toolbar), 5);
  162.    gtk_toolbar_set_space_size(GTK_TOOLBAR(toolbar), 5);
  163.  
  164.    data->arrow_up = make_toolbar_icon(toolbar, window, arrow_up_xpm, "MoveUp",
  165.                       _("Move Up"), selection_command, 
  166.                       &data->cmd_move_up);
  167.    data->arrow_down = make_toolbar_icon(toolbar, window, arrow_down_xpm, 
  168.                     "MoveDown", _("Move Down"), 
  169.                     selection_command, 
  170.                     &data->cmd_move_down);
  171.    gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
  172.    data->edit = make_toolbar_icon(toolbar, window, edit_xpm, "Edit",
  173.                   _("Edit"), selection_command, 
  174.                   &data->cmd_edit);
  175.    gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
  176.    data->remove = make_toolbar_icon(toolbar, window, delete_xpm, "Delete",
  177.                     _("Delete"), selection_command, 
  178.                     &data->cmd_delete);
  179.  
  180.    gtk_widget_show(toolbar);
  181.  
  182.    return toolbar;
  183. }
  184.  
  185. static void
  186. selection_update(Selection_t *selection, gint row, Object_t *obj)
  187. {
  188.    GdkBitmap *mask;
  189.    GdkPixmap *icon = object_get_icon(obj, selection->list, &mask);
  190.  
  191.    gtk_clist_set_pixtext(GTK_CLIST(selection->list), row, 1, obj->url, 8,
  192.              icon, mask);
  193.    gtk_clist_set_text(GTK_CLIST(selection->list), row, 2, obj->target);
  194.    gtk_clist_set_text(GTK_CLIST(selection->list), row, 3, obj->comment);
  195. }
  196.  
  197. static void
  198. selection_renumber(Selection_t *selection, gint row)
  199. {
  200.    for (; row < selection->nr_rows; row++) {
  201.       char scratch[16];
  202.       sprintf(scratch, "%d", row + 1);
  203.       gtk_clist_set_text(GTK_CLIST(selection->list), row, 0, scratch);
  204.    }
  205. }
  206.  
  207. static void
  208. selection_set_selected(Selection_t *selection, gint row)
  209. {
  210.    Object_t *obj = gtk_clist_get_row_data(GTK_CLIST(selection->list), row);
  211.  
  212.    if (obj->selected) {
  213.       selection->select_lock = TRUE;
  214.       gtk_clist_select_row(GTK_CLIST(selection->list), row, -1);
  215.    } else {
  216.       selection->unselect_lock = TRUE;
  217.       gtk_clist_unselect_row(GTK_CLIST(selection->list), row, -1);
  218.    }
  219. }
  220.  
  221. static void
  222. object_added_cb(Object_t *obj, gpointer data)
  223. {
  224.    Selection_t *selection = (Selection_t*) data;
  225.    gint row = object_get_position_in_list(obj);
  226.    char scratch[16];
  227.    gchar *text[4];
  228.  
  229.    selection->nr_rows++;
  230.    
  231.    text[0] = scratch;
  232.    text[1] = obj->url;
  233.    text[2] = obj->target;
  234.    text[3] = obj->comment;
  235.    
  236.    if (row < selection->nr_rows - 1) {
  237.       sprintf(scratch, "%d", row + 1);
  238.       gtk_clist_insert(GTK_CLIST(selection->list), row, text);
  239.       selection_renumber(selection, row);
  240.    } else {
  241.       sprintf(scratch, "%d", selection->nr_rows);
  242.       gtk_clist_append(GTK_CLIST(selection->list), text);
  243.    }
  244.    selection_update(selection, row, obj);
  245.    gtk_clist_set_row_data(GTK_CLIST(selection->list), row, (gpointer) obj);
  246.    selection_set_selected(selection, row);
  247. }
  248.  
  249. static gint
  250. selection_find_object(Selection_t *selection, Object_t *obj)
  251. {
  252.    return gtk_clist_find_row_from_data(GTK_CLIST(selection->list),
  253.                        (gpointer) obj);
  254. }
  255.  
  256. static void
  257. object_updated_cb(Object_t *obj, gpointer data)
  258. {
  259.    Selection_t *selection = (Selection_t*) data;
  260.    gint row = selection_find_object(selection, obj);
  261.    selection_update(selection, row, obj);
  262. }
  263.  
  264. static void
  265. object_removed_cb(Object_t *obj, gpointer data)
  266. {
  267.    Selection_t *selection = (Selection_t*) data;
  268.    gint row = selection_find_object(selection, obj);
  269.  
  270.    selection->unselect_lock = TRUE;
  271.    gtk_clist_unselect_row(GTK_CLIST(selection->list), row, -1);
  272.    gtk_clist_remove(GTK_CLIST(selection->list), row);
  273.    selection_renumber(selection, row);
  274.    selection->selected_child = NULL;
  275.    selection->nr_rows--;
  276.    set_buttons(selection);
  277. }
  278.  
  279. static void
  280. object_selected_cb(Object_t *obj, gpointer data)
  281. {
  282.    Selection_t *selection = (Selection_t*) data;
  283.    gint row = selection_find_object(selection, obj);
  284.  
  285.    selection_set_selected(selection, row);
  286. }
  287.  
  288. static void
  289. object_moved_cb(Object_t *obj, gpointer data)
  290. {
  291.    Selection_t *selection = (Selection_t*) data;
  292.    gint row = object_get_position_in_list(obj);
  293.    selection->select_lock = TRUE;
  294.    gtk_clist_set_row_data(GTK_CLIST(selection->list), row, (gpointer) obj);
  295.    selection_set_selected(selection, row);
  296.    selection_update(selection, row, obj);
  297. }
  298.  
  299. static void
  300. toggle_order(GtkWidget *widget, gint column, gpointer data)
  301. {
  302.    /* Fix me! */
  303. }
  304.  
  305. static GtkTargetEntry target_table[] = {
  306.    {"STRING", 0, 1 },
  307.    {"text/plain", 0, 2 }
  308. };
  309.  
  310. static void 
  311. handle_drop(GtkWidget *widget, GdkDragContext *context, gint x, gint y, 
  312.         GtkSelectionData *data, guint info, guint time)
  313. {
  314.    gboolean success = FALSE;
  315.    if (data->length >= 0 && data->format == 8) {
  316.       gint row, column;
  317.       if (gtk_clist_get_selection_info(GTK_CLIST(widget), x, y, &row, 
  318.                        &column)) {
  319.      Object_t *obj = gtk_clist_get_row_data(GTK_CLIST(widget), row + 1);
  320.      if (!obj->locked) {
  321.         command_list_add(edit_object_command_new(obj));
  322.         object_set_url(obj, data->data);
  323.         object_emit_update_signal(obj);
  324.         success = TRUE;
  325.      }
  326.       }
  327.    }
  328.    gtk_drag_finish(context, success, FALSE, time);
  329. }
  330.  
  331. Selection_t*
  332. make_selection(GtkWidget *window, ObjectList_t *object_list)
  333. {
  334.    Selection_t *data = g_new(Selection_t, 1);
  335.    GtkWidget *swin, *frame, *hbox;
  336.    GtkWidget *toolbar;
  337.    GtkWidget *list;
  338.    gchar     *titles[] = {"#", N_("URL"), N_("Target"), N_("Comment")};
  339.    gint      i;
  340.  
  341.    data->object_list = object_list;
  342.    data->selected_child = NULL;
  343.    data->is_visible = TRUE;
  344.    data->nr_rows = 0;
  345.    data->select_lock = FALSE;
  346.    data->unselect_lock = FALSE;
  347.  
  348.    data->container = frame = gtk_frame_new(NULL);
  349.    gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
  350.    gtk_widget_show(frame);
  351.  
  352.    hbox = gtk_hbox_new(FALSE, 1);
  353.    gtk_container_add(GTK_CONTAINER(frame), hbox); 
  354.    gtk_widget_show(hbox);
  355.  
  356.    toolbar = make_selection_toolbar(data, window);
  357.    gtk_container_add(GTK_CONTAINER(hbox), toolbar);
  358.  
  359.    /* Create selection */
  360.    frame = gtk_frame_new(_("Selection"));
  361.    gtk_container_set_border_width(GTK_CONTAINER(frame), 10);
  362.    gtk_container_add(GTK_CONTAINER(hbox), frame);
  363.    gtk_widget_show(frame);
  364.  
  365.    for (i = 0; i < 4; i++)
  366.      titles[i] = gettext(titles[i]);
  367.    data->list = list = gtk_clist_new_with_titles(4, titles);
  368.    GTK_WIDGET_UNSET_FLAGS(data->list, GTK_CAN_FOCUS);
  369.    gtk_clist_column_titles_passive(GTK_CLIST(list));
  370.    gtk_clist_column_title_active(GTK_CLIST(list), 0);
  371.    gtk_clist_set_column_width(GTK_CLIST(list), 0, 16);
  372.    gtk_clist_set_column_width(GTK_CLIST(list), 1, 80);
  373.    gtk_clist_set_column_width(GTK_CLIST(list), 2, 64);
  374.    gtk_clist_set_column_width(GTK_CLIST(list), 3, 64);
  375. /*   gtk_clist_set_reorderable(GTK_CLIST(list), TRUE); */
  376.  
  377.    /* Create scrollable window */
  378.    swin = gtk_scrolled_window_new(NULL, NULL);
  379.    gtk_widget_set_usize(swin, 16 + 80 + 2 * 64 + 16, -1);
  380.    gtk_container_add(GTK_CONTAINER(frame), swin);
  381.    gtk_widget_show(swin);
  382.  
  383.    gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(swin), list);
  384.    gtk_widget_show(list);
  385.  
  386.    /* Drop support */
  387.    gtk_drag_dest_set(list, GTK_DEST_DEFAULT_ALL, target_table,
  388.              2, GDK_ACTION_COPY);
  389.    gtk_signal_connect(GTK_OBJECT(list), "drag_data_received",
  390.               GTK_SIGNAL_FUNC(handle_drop), NULL);
  391.  
  392.    /* Callbacks we are interested in */
  393.    gtk_signal_connect(GTK_OBJECT(list), "click_column",
  394.               GTK_SIGNAL_FUNC(toggle_order), data);
  395.    gtk_signal_connect(GTK_OBJECT(list), "select_row",
  396.               GTK_SIGNAL_FUNC(select_row_cb), data);
  397.    gtk_signal_connect(GTK_OBJECT(list), "unselect_row",
  398.               GTK_SIGNAL_FUNC(unselect_row_cb), data);
  399.    gtk_signal_connect(GTK_OBJECT(list), "row_move",
  400.               GTK_SIGNAL_FUNC(row_move_cb), data);
  401.  
  402.    /* For handling doubleclick */
  403.    gtk_signal_connect(GTK_OBJECT(list), "button_press_event",
  404.               GTK_SIGNAL_FUNC(button_press_cb), data);
  405.    gtk_signal_connect(GTK_OBJECT(list), "button_release_event",
  406.               GTK_SIGNAL_FUNC(button_release_cb), data);
  407.  
  408.    gtk_clist_set_selection_mode(GTK_CLIST(list), GTK_SELECTION_MULTIPLE);
  409.  
  410.    set_buttons(data);
  411.  
  412.    /* Set object list callbacks we're interested in */
  413.    object_list_add_add_cb(object_list, object_added_cb, data);
  414.    object_list_add_update_cb(object_list, object_updated_cb, data);
  415.    object_list_add_remove_cb(object_list, object_removed_cb, data);
  416.    object_list_add_select_cb(object_list, object_selected_cb, data);
  417.    object_list_add_move_cb(object_list, object_moved_cb, data);
  418.  
  419.    return data;
  420. }
  421.  
  422. void
  423. selection_toggle_visibility(Selection_t *selection)
  424. {
  425.    if (selection->is_visible) {
  426.       gtk_widget_hide(selection->container);
  427.       selection->is_visible = FALSE;
  428.    } else {
  429.       gtk_widget_show(selection->container);
  430.       selection->is_visible = TRUE;
  431.    }
  432. }
  433.